package com.mastercard.labs

import org.springframework.dao.DataIntegrityViolationException

class VoucherController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def voucherService;
	
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        log.info "VoucherConroller list()"
		if(params.prevAction == "dashboard"){
			redirect(controller: "dashboard")
		}else if(params.search == "Search"){
			log.info "VoucherConroller search()"
			def voucherInstanceList = voucherService.searchByCondition(params.searchTitle, params.searchCountry, params.searchMerchant, params.searchCategories, null);
			
			[voucherInstanceList: voucherInstanceList,
			searchTitle: params.searchTitle,
			searchMerchant: params.searchMerchant,
			searchCategories: params.searchCategories,
			searchCountry: params.searchCountry
			]
		}
    }

    def create() {
        [voucherInstance: new Voucher(params)]
    }

    def save() {
		log.info "VoucherConroller save()"
        def voucherInstance = new Voucher(params)
        if (!voucherInstance.save(flush: true)) {
            render(view: "create", model: [voucherInstance: voucherInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'voucher.label', default: 'Voucher'), voucherInstance.title])
        redirect(action: "create", id: voucherInstance.id)
    }

    def show() {
		log.info "VoucherConroller show()"
        def voucherInstance = Voucher.get(params.id)
        if (!voucherInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.id])
            redirect(action: "list")
            return
        }

        [voucherInstance: voucherInstance]
    }

    def edit() {
		log.info "VoucherConroller edit()"
        def voucherInstance = Voucher.get(params.id)
        if (!voucherInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.id])
            redirect(action: "list")
            return
        }

        [voucherInstance: voucherInstance, prevAction: params.prevAction]
    }
	
	def validateStartDate(obj) {
		Date oriDate = obj.getStartDate()
		log.info 'db start date:' + oriDate
		log.info 'input start date:' + params.startDate_value
		log.info 'input db start date:' + params.dbStartDate
		if (params.startDate_value == null || '' == params.startDate_value) return true
		return Date.parse("MM/dd/yyyy", params.startDate_value).after(oriDate -1)
	}

    def update() {
		log.info "VoucherConroller update()"
        def voucherInstance = Voucher.get(params.id)
        if (!voucherInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.id])
            redirect(action: "list")
            return
        }
		
		if (!validateStartDate(voucherInstance)){
			//flash.message = message(code: 'default.not.found.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
			//voucherInstance.errors.addError("startDate", "something bad happened")
			
			voucherInstance.errors.rejectValue(
				'startDate',
				'user.startDate.doesnotmatch',
				'start Date should be not before than  [' + voucherInstance.getStartDate().format("MM/dd/yyyy") + ']')
			
			render(view: "edit", model: [voucherInstance: voucherInstance])
			return
		}

        if (params.version) {
            def version = params.version.toLong()
            if (voucherInstance.version > version) {
                voucherInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'voucher.label', default: 'Voucher')] as Object[],
                          "Another user has updated this Voucher while you were editing")
                render(view: "edit", model: [voucherInstance: voucherInstance])
                return
            }
        }

		if(!params."merchant.id" || params."merchant.id" == "") {
//			params.merchant == null
			params.remove("merchant.id")
			voucherInstance.setMerchant(null)
		}
		
        voucherInstance.properties = params

        if (!voucherInstance.save(flush: true)) {
            render(view: "edit", model: [voucherInstance: voucherInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'voucher.label', default: 'Voucher'), voucherInstance.title])
        redirect(action: "edit", id: voucherInstance.id)
    }

    def delete() {
		log.info "VoucherConroller delete()"
        def voucherInstance = Voucher.get(params.id)
        if (!voucherInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.id])
            redirect(action: "list")
            return
        }

        try {
            voucherInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.title])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'voucher.label', default: 'Voucher'), params.title])
            redirect(action: "show", id: params.id)
        }
    }
}
